home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Windows 95 with MFC
/
Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso
/
CODE
/
Chap13
/
GridDemo
/
GridDemo.cpp
next >
Wrap
C/C++ Source or Header
|
1996-04-05
|
5KB
|
174 lines
//***********************************************************************
//
// GridDemo.cpp
//
//***********************************************************************
#include <afxwin.h>
#include <afxcmn.h>
#include "Resource.h"
#include "GridDemo.h"
CMyApp myApp;
/////////////////////////////////////////////////////////////////////////
// CMyApp member functions
BOOL CMyApp::InitInstance ()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////
// CMainWindow message map and member functions
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_PAINT ()
ON_COMMAND (IDM_OPTIONS_SETTINGS, OnOptionsSettings)
ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
END_MESSAGE_MAP ()
CMainWindow::CMainWindow ()
{
m_cx = m_cy = 8;
m_nWeight = 4;
Create (NULL, "Grid Demo", WS_OVERLAPPEDWINDOW, rectDefault,
NULL, MAKEINTRESOURCE (IDR_MAINFRAME));
}
void CMainWindow::OnPaint ()
{
CRect rect;
GetClientRect (&rect);
int nShade = m_nWeight * 32;
if (nShade != 0)
nShade--;
CPaintDC dc (this);
CPen pen (PS_SOLID, 1, RGB (nShade, nShade, nShade));
CPen* pOldPen = dc.SelectObject (&pen);
int x;
for (int i=1; i<m_cx; i++) {
x = (rect.Width () * i) / m_cx;
dc.MoveTo (x, 0);
dc.LineTo (x, rect.Height ());
}
int y;
for (i=1; i<m_cy; i++) {
y = (rect.Height () * i) / m_cy;
dc.MoveTo (0, y);
dc.LineTo (rect.Width (), y);
}
dc.SelectObject (pOldPen);
}
void CMainWindow::OnOptionsSettings ()
{
CSettingsDialog dlg (this);
dlg.m_cx = m_cx;
dlg.m_cy = m_cy;
dlg.m_nWeight = m_nWeight;
if (dlg.DoModal () == IDOK) {
m_cx = dlg.m_cx;
m_cy = dlg.m_cy;
m_nWeight = dlg.m_nWeight;
Invalidate ();
}
}
void CMainWindow::OnOptionsExit ()
{
SendMessage (WM_CLOSE, 0, 0);
}
/////////////////////////////////////////////////////////////////////////
// CSettingsDialog message map and member functions
BOOL CSettingsDialog::OnInitDialog ()
{
CDialog::OnInitDialog ();
// Initialize the slider
CSliderCtrl* pSlider = (CSliderCtrl*) GetDlgItem (IDC_SLIDER);
pSlider->SetRange (0, 8);
pSlider->SetPos (m_nWeight);
// Initialize the first spin button
CSpinButtonCtrl* pSpin = (CSpinButtonCtrl*) GetDlgItem (IDC_SPINHORZ);
pSpin->SetRange (2, 64);
// Initialize the second spin button
pSpin = (CSpinButtonCtrl*) GetDlgItem (IDC_SPINVERT);
pSpin->SetRange (2, 64);
// Create and initialize a tooltip control
m_ctlTT.Create (this);
m_ctlTT.AddWindowTool (GetDlgItem (IDC_SLIDER),
MAKEINTRESOURCE (IDS_SLIDER));
m_ctlTT.AddWindowTool (GetDlgItem (IDC_EDITHORZ),
MAKEINTRESOURCE (IDS_EDITHORZ));
m_ctlTT.AddWindowTool (GetDlgItem (IDC_EDITVERT),
MAKEINTRESOURCE (IDS_EDITVERT));
return TRUE;
}
void CSettingsDialog::OnOK ()
{
CSliderCtrl* pSlider = (CSliderCtrl*) GetDlgItem (IDC_SLIDER);
m_nWeight = pSlider->GetPos ();
CDialog::OnOK ();
}
void CSettingsDialog::DoDataExchange (CDataExchange* pDX)
{
CDialog::DoDataExchange (pDX);
DDX_Text (pDX, IDC_EDITHORZ, m_cx);
DDV_MinMaxInt (pDX, m_cx, 2, 64);
DDX_Text (pDX, IDC_EDITVERT, m_cy);
DDV_MinMaxInt (pDX, m_cy, 2, 64);
}
/////////////////////////////////////////////////////////////////////////
// CMyToolTipCtrl member functions
BOOL CMyToolTipCtrl::AddWindowTool (CWnd* pWnd, LPCTSTR pszText)
{
TOOLINFO ti;
ti.cbSize = sizeof (TOOLINFO);
ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
ti.hwnd = pWnd->GetParent ()->GetSafeHwnd ();
ti.uId = (UINT) pWnd->GetSafeHwnd ();
ti.hinst = AfxGetInstanceHandle ();
ti.lpszText = (LPTSTR) pszText;
return (BOOL) SendMessage (TTM_ADDTOOL, 0, (LPARAM) &ti);
}
BOOL CMyToolTipCtrl::AddRectTool (CWnd* pWnd, LPCTSTR pszText,
LPCRECT lpRect, UINT nIDTool)
{
TOOLINFO ti;
ti.cbSize = sizeof (TOOLINFO);
ti.uFlags = TTF_SUBCLASS;
ti.hwnd = pWnd->GetSafeHwnd ();
ti.uId = nIDTool;
ti.hinst = AfxGetInstanceHandle ();
ti.lpszText = (LPTSTR) pszText;
::CopyRect (&ti.rect, lpRect);
return (BOOL) SendMessage (TTM_ADDTOOL, 0, (LPARAM) &ti);
}